package com.njtoyo.taxi.admin.service.business.driver.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.google.common.collect.Lists;
import com.njtoyo.taxi.admin.cache.PrimaryCachePutter;
import com.njtoyo.taxi.admin.library.Const;
import com.njtoyo.taxi.admin.library.common.OperationRecordHelper;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverNotifySystemMessageTrigger;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverNotifySystemMessageTriggerMapper;
import com.njtoyo.taxi.admin.rest.presenter.business.driver.system_message.DriverSystemMessageDetailPresenter;
import com.njtoyo.taxi.admin.rest.presenter.business.driver.system_message.ListPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.driver.system_message.EditWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.driver.system_message.ListWrapper;
import com.njtoyo.taxi.admin.service.business.driver.SystemMessageService;
import com.taxi.entity.common.RestResult;
import com.taxi.entity.common.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@DS(Const.PRIMARY_DS)
public class SystemMessageServiceImpl implements SystemMessageService {

    @Autowired
    private DriverNotifySystemMessageTriggerMapper driverSystemMessageMapper;

    @Autowired
    private PrimaryCachePutter primaryCachePutter;

    @Autowired
    private OperationRecordHelper operationRecordHelper;

    @Override
    public RestResult getList(ListWrapper listWrapper) {
        listWrapper.setOrders(Lists.newArrayList(new OrderItem("id", false)));
        IPage<ListPresenter> res = driverSystemMessageMapper.selectPage(listWrapper);

        return RestResult.success(res);
    }

    @Override
    public RestResult deleteCache() {
        primaryCachePutter.deleteDriverSystemMessage();
        operationRecordHelper.addBusinessLog("清除司机系统消息缓存");
        return RestResult.success();
    }

    @Override
    public RestResult edit(EditWrapper editWrapper) {
        QueryWrapper<DriverNotifySystemMessageTrigger> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", editWrapper.getCategory());
        DriverNotifySystemMessageTrigger message = driverSystemMessageMapper.selectOne(queryWrapper);
        if (Objects.isNull(message)) {
            message = new DriverNotifySystemMessageTrigger();
            message.setContent(editWrapper.getContent());
            message.setCategory(editWrapper.getCategory());
            message.setRule(JSON.toJSONString(editWrapper.getRule()));
            driverSystemMessageMapper.insert(message);
            operationRecordHelper.addBusinessLog(String.format("新增司机系统消息 content:[%s],category:[%s],rule:[%s]", message.getContent(), message.getCategory(), message.getRule()));
        } else {
            message.setRule(JSON.toJSONString(editWrapper.getRule()));
            message.setContent(editWrapper.getContent());
            driverSystemMessageMapper.updateById(message);
            operationRecordHelper.addBusinessLog(String.format("修改司机系统消息 [%d],content:[%s],rule:[%s]", message.getId(), message.getContent(), message.getRule()));
        }

        return RestResult.success();
    }

    @Override
    public RestResult delete(Long id) {
        DriverNotifySystemMessageTrigger message = driverSystemMessageMapper.selectById(id);
        if (Objects.isNull(message)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        driverSystemMessageMapper.deleteById(id);
        operationRecordHelper.addBusinessLog(String.format("删除司机系统消息 [%d]", id));

        return RestResult.success();
    }

    @Override
    public RestResult<DriverSystemMessageDetailPresenter> getDetail(Long id) {
        // 判断消息是否存在
        DriverNotifySystemMessageTrigger item = driverSystemMessageMapper.selectById(id);
        if (Objects.isNull(item)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }

        DriverSystemMessageDetailPresenter res = BeanUtil.copyProperties(item, DriverSystemMessageDetailPresenter.class);
        if (ObjectUtil.isNotEmpty(item.getRule())) {
            Object rule = JSON.parse(item.getRule());
            res.setRule(rule);
        }

        return RestResult.success(res);
    }
}
